﻿using Core.Framework;
using iTool.ClusterComponent;
using Orleans.Streams;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Silo.CloudService
{
    public class SubscribeStreamProvider : SubscribeQueueHandler<string>
    {
        public event EventHandler<KeyValuePair<string, string>> Handler;

        public SubscribeStreamProvider(string topic) : base(topic, AppSetting.QueueServiceStreamNamespace)
        {

        }

        public override Task OnErrorAsync(Exception ex)
        {
            return Task.CompletedTask;
        }

        public override Task OnMessageAsync(string message, StreamSequenceToken token)
        {
            try
            {
                this.Handler.Invoke(this, message.TryToEntity<KeyValuePair<string, string>>());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Task.CompletedTask;
        }
    }
}
